<template>
  <div>
    <div class="login">
      <div class="bg">
        <h3>Kueiq音乐</h3>
        <el-card class="login-card">
    <el-form ref="form" v-model="loginForm" label-width="80px" label-position="right" size="mini">

      <el-form-item label="手机号码：" prop="name" label-width="100px">
        <input type="text" v-model="loginForm.phoneNum" class="loginInput" />
      </el-form-item>
      <el-form-item label="密码：" prop="pass" label-width="100px">
        <input type="password" v-model="loginForm.password" class="loginInput" />
      </el-form-item>

      <div class="loginButton">
        <el-button type="danger" @click="login" size="mini">登录</el-button>
      </div>
      <div class="regEnter" @click="registered">注册</div>
    </el-form>
        </el-card>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  name: "Login",
  props: {
    isRegisteredShow: {
      type: Boolean,
      default: false,
    }
  },
  data () {
    return {
      loginForm: {
        password: "",
        phoneNum: "",
      },
    };
  },
  methods: {



    registered (){
      this.$router.push("/Registered")
    },
    // 事件
    // 点击登录按钮的回调
    login () {
      // console.log("提交表单");
      //   console.log(this.loginForm);
      this.loginByCellphone();
    },

    //   手机登录请求
    async loginByCellphone () {
      let timestamp = Date.parse(new Date());
      let result = await this.$request("/login/cellphone", {
        phone: this.loginForm.phoneNum,
        password: this.loginForm.password,
        withCredentials: true,
        timestamp,
      });
      // console.log(result);
      // 登录成功
      if (result.data.code == 200) {
        // 将请求到的用户id存入localstorage
        // 在index.vue 和 bottomControl.vue中用到过
        window.localStorage.setItem("userId", result.data.profile.userId);
        // this.userInfo = result.data.profile;
        // 将userInfo传回父组件
        this.$emit("getUserInfo", result.data.profile);

        this.$message.success("登录成功!");
        // 刷新页面
        // this.$router.go(0);
        this.$router.go(-1);
        // 修改vuex中的登录状态
        this.$store.commit("updataLoginState", true);
        // this.$store.commit("updateCurrentUserId", result.data.profile.userId);
      } else if (result.data.code == 400) {
        // 手机号错误
        this.$message.error("手机号错误!");
        return;
      } else if (result.data.code == 502) {
        // 密码错误
        this.$message.error("密码错误!");
        return;
      } else {
        // 登录失败，请稍后重试
        this.$message.error("登录失败，请稍后重试!");
        this.$router.go(0);
        return;
      }
      // 清空输入框的内容
      this.loginForm.password = "";
      this.loginForm.phoneNum = "";
    },
  },
};
</script>

<style scoped>
.loginButton{
  text-align: center;
}
.regEnter{
  text-align: center;
}

.regEnter:hover {
  text-decoration: underline;

}

.loginInput {
  height: 25px;
  border-radius: 5px;
  outline: none;
  border: 1px solid #999;
  padding-left: 5px;
  width: 200px;
}

.login {
  width: 100%;
  height: 100%;
  background-image: url("./../img/background/login1.jpg");
  background-size: cover;
  position: absolute;
  background-repeat: no-repeat;
  z-index: -1;
}
.login .bg{
  position: absolute;
  margin: auto;
  top: 50%;
  left: 50%;
  width: 500px;
  height: 480px;
  transform: translate(-50%, -50%);
}
.login .bg h3{
  text-align: center;
  color: #ffffff;
  font-size: 2.4em;
  letter-spacing: 3px;
  text-shadow: 2px 2px 5px #cccccc;
}
.login-card {
  position: absolute;
  margin: auto;
  width: 450px;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  padding: 45px 45px;
  border-radius: 3%;
}
</style>
